package com.cardone.platform.authority.dao;

import java.util.List;
import java.util.Map;
import java.util.UUID;

import lombok.Getter;
import lombok.Setter;

import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;

import com.cardone.common.dto.PaginationDto;
import com.cardone.common.util.EntityUtils;
import com.cardone.context.Attributes;
import com.cardone.context.ContextHolder;
import com.cardone.persistent.builder.Model;
import com.cardone.persistent.builder.ModelArgs;
import com.cardone.persistent.builder.ModelUtils;
import com.cardone.persistent.support.JdbcTemplateSupport;
import com.cardone.persistent.support.PaginationArgs;
import com.cardone.platform.authority.dto.RoleDto;
import com.google.common.collect.Maps;

/**
 * 角色
 *
 * @author yaohaitao
 *
 */
@Getter
@Setter
public class RoleJdbcDao implements RoleDao {
	@Override
	public int deleteByIds(final String ids) {
		final Model model = ModelUtils.newModel();

		model.put(Attributes.ids.name(), ids);

		return ContextHolder.getBean(JdbcTemplateSupport.class).update(SqlIds.deleteByIds.id(), model);
	}

	@Override
	public <P> P findByCodeOrId(final Class<P> mappedClass, final RoleDto findRole) {
		final String[] whereAndEqProperties = EntityUtils.getWhereProperties(findRole, Attributes.orgId.name(), Attributes.departmentId.name(), Attributes.code.name());

		final Model model = ModelUtils.newModel(new ModelArgs(findRole, whereAndEqProperties));

		if (MapUtils.isNotEmpty(findRole.getAttrs())) {
			model.putAll(findRole.getAttrs());
		}

		model.putTrue(Model.Keys.whereAndBetween.stringValue(), Attributes.sysdate.name());

		return ContextHolder.getBean(JdbcTemplateSupport.class).find(mappedClass, SqlIds.findByCode.id(), model);
	}

	@Override
	public <P> P findById(final Class<P> mappedClass, final RoleDto findRole) {
		final String[] whereAndEqProperties = EntityUtils.getWhereProperties(findRole);

		final Model model = ModelUtils.newModel(new ModelArgs(findRole, whereAndEqProperties).setIsSimple(true));

		if (MapUtils.isNotEmpty(findRole.getAttrs())) {
			model.putAll(findRole.getAttrs());
		}

		return ContextHolder.getBean(JdbcTemplateSupport.class).find(mappedClass, SqlIds.findById.id(), model);
	}

	@Override
	public <P> List<P> findListByLikeCode(final Class<P> mappedClass, final RoleDto findListRole) {
		final Model model = ModelUtils.newModel(new ModelArgs(findListRole).setIsSimple(true));

		if (MapUtils.isNotEmpty(findListRole.getAttrs())) {
			model.putAll(findListRole.getAttrs());
		}

		return ContextHolder.getBean(JdbcTemplateSupport.class).findList(mappedClass, SqlIds.findByLikeCode.id(), model);
	}

	@Override
	public <P> List<P> findListByUserId(final Class<P> mappedClass, final String userId) {
		final Map<String, Object> model = Maps.newHashMap();

		model.put(Attributes.userId.name(), userId);

		return ContextHolder.getBean(JdbcTemplateSupport.class).findList(mappedClass, SqlIds.findByUserId.id(), model);
	}

	@Override
	public int insertByCode(final RoleDto insertRole) {
		if (StringUtils.isBlank(insertRole.getId())) {
			insertRole.setId(UUID.randomUUID().toString());
		}

		final Model model = ModelUtils.newModel(new ModelArgs(Model.Keys.insert.stringValue(), insertRole));

		if (MapUtils.isNotEmpty(insertRole.getAttrs())) {
			model.putAll(insertRole.getAttrs());
		}

		model.putTrue(Model.Keys.whereAndBetween.stringValue(), Attributes.sysdate.name());

		return ContextHolder.getBean(JdbcTemplateSupport.class).update(SqlIds.insertByCode.id(), model);
	}

	@Override
	public <P> PaginationDto<P> paginationByLikeCode(final Class<P> mappedClass, final RoleDto paginationRole) {
		final Model model = ModelUtils.newModel(new ModelArgs(paginationRole).setIsSimple(true));

		return ContextHolder.getBean(JdbcTemplateSupport.class).pagination(mappedClass, new PaginationArgs(SqlIds.readByLikeCode.id(), SqlIds.findByLikeCode.id(), paginationRole), model);
	}

	@Override
	public Integer readByCodeNqIdForCount(final RoleDto readRole) {
		final String[] whereAndEqProperties = { Attributes.code.name() };

		final Model model = ModelUtils.newModel(new ModelArgs(readRole, whereAndEqProperties));

		if (MapUtils.isNotEmpty(readRole.getAttrs())) {
			model.putAll(readRole.getAttrs());
		}

		final String[] whereAndNqProperties = EntityUtils.getWhereProperties(readRole);

		if (ArrayUtils.isNotEmpty(whereAndNqProperties)) {
			ModelUtils.put(model, new ModelArgs(Model.Keys.whereAndNq.stringValue(), readRole, whereAndNqProperties));
		}

		model.putTrue(Model.Keys.whereAndBetween.stringValue(), Attributes.sysdate.name());

		return ContextHolder.getBean(JdbcTemplateSupport.class).read(Integer.class, SqlIds.readByCode.id(), model);
	}

	@Override
	public String readIdByCode(final RoleDto readRole) {
		final Map<String, Object> model = Maps.newHashMap();

		model.put(Attributes.code.name(), readRole.getCode());

		return ContextHolder.getBean(JdbcTemplateSupport.class).read(String.class, SqlIds.readIdByCode.id(), model);
	}

	@Override
	public <P> P saveByIdOrCode(final Class<P> mappedClass, final RoleDto saveRole) {
		final P oldRole = this.findByCodeOrId(mappedClass, saveRole);

		if (oldRole == null) {
			this.insertByCode(saveRole);

			return this.findByCodeOrId(mappedClass, saveRole);
		}

		final int updateCount = this.updateByCode(saveRole, oldRole);

		if (updateCount < 1) {
			return oldRole;
		}

		return this.findByCodeOrId(mappedClass, saveRole);
	}

	@Override
	public int updateByCode(final RoleDto updateRole) {
		final RoleDto oldRole = this.findByCodeOrId(RoleDto.class, updateRole);

		return this.updateByCode(updateRole, oldRole);
	}

	private <P> int updateByCode(final RoleDto updateRole, final P oldRole) {
		if (oldRole == null) {
			return 0;
		}

		final String[] useProperties = EntityUtils.addNotNullTimeSegmentProperties(new String[] { Attributes.orgId.name(), Attributes.departmentId.name(), Attributes.code.name(), Attributes.name.name(), Attributes.value.name(), Attributes.orderNum.name() }, updateRole);

		final String[] updateProperties = com.cardone.common.util.BeanUtils.diffProperties(updateRole, oldRole, useProperties);

		if (ArrayUtils.isEmpty(updateProperties)) {
			return 0;
		}

		final String[] whereAndEqProperties = EntityUtils.getWhereProperties(oldRole, Attributes.id.name());

		com.cardone.common.util.BeanUtils.copyProperties(oldRole, updateRole, whereAndEqProperties);

		final Model model = ModelUtils.newModel(new ModelArgs(Model.Keys.update.stringValue(), updateRole, updateProperties), new ModelArgs(updateRole, whereAndEqProperties));

		if (MapUtils.isNotEmpty(updateRole.getAttrs())) {
			model.putAll(updateRole.getAttrs());
		}

		return ContextHolder.getBean(JdbcTemplateSupport.class).update(SqlIds.updateByCode.id(), model);
	}
}